package com.example.lib_cache.viewmodel;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.baidu.mapapi.model.LatLng;
import com.example.lib_cache.model.Address;
import com.example.lib_cache.model.Application;
import com.example.lib_cache.model.Guide;
import com.example.lib_cache.model.Navigation;
import com.example.lib_cache.model.Order;
import com.example.lib_cache.model.User;
import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_cache.network.exception.ErrorConsumer;
import com.example.lib_cache.network.response.ResponseResultTransformer;
import com.example.lib_cache.service.ApiService;
import com.example.lib_common.util.LiveDataManager;

import io.reactivex.functions.Consumer;

public class TravelActivityViewModel extends ViewModel {

    private final static String TAG = TravelActivityViewModel.class.getSimpleName();

    private MutableLiveData<Order> orderMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<Guide> guideMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<Navigation> navigationMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<Boolean> isGetOff = new MutableLiveData<>();


    private LatLng orderLL = null;
    private LatLng newLL = null;
    private boolean swap = false;

    private MutableLiveData<User> userMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<Application> applicationMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<User> getUserMutableLiveData() {
        return userMutableLiveData;
    }

    public void setUserMutableLiveData(User user) {
        this.userMutableLiveData.postValue(user);
    }

    public MutableLiveData<Application> getApplicationMutableLiveData() {
        return applicationMutableLiveData;
    }

    public void setApplicationMutableLiveData(Application application) {
        this.applicationMutableLiveData.postValue(application);
    }

    public MutableLiveData<Order> getOrderMutableLiveData() {
        return orderMutableLiveData;
    }

    public void setOrderMutableLiveData(Order order) {
        this.orderMutableLiveData.postValue(order);
    }

    public MutableLiveData<Guide> getGuideMutableLiveData() {
        return guideMutableLiveData;
    }

    public void setGuideMutableLiveData(Guide guide) {
        this.guideMutableLiveData.postValue(guide);
    }

    public MutableLiveData<Navigation> getNavigationMutableLiveData() {
        return navigationMutableLiveData;
    }
    public void setNavigationMutableLiveData(Navigation navigation) {
        this.navigationMutableLiveData.postValue(navigation);
    }

    public LatLng getOrderLL() {
        return orderLL;
    }

    public LatLng getNewLL() {
        return newLL;
    }

    public boolean getSwap() {
        return swap;
    }

    public void setSwap(boolean toSwap) {
        this.swap = toSwap;
    }

    public MutableLiveData<Boolean> getIsGetOff() {
        return isGetOff;
    }

    public void setIsGetOff(boolean isGetOff) {
        this.isGetOff.postValue(isGetOff);
    }

    public void swapLatLng(Double longitude, Double latitude){
        if(swap) {
            orderLL = newLL;
            newLL = new LatLng(latitude, longitude);
            return;
        }
        orderLL = new LatLng(latitude,longitude);
        newLL = new LatLng(latitude,longitude);
    }

    public void requestApplication(ApiService apiService,Context context,Long driverId){
        apiService.getDriverCarInfo(driverId)
                .compose(ResponseResultTransformer.<Application>obtain())
                .subscribe(new Consumer<Application>() {
                    @Override
                    public void accept(Application application) throws Exception {
                        Log.d(TAG,"请求成功");
                        setApplicationMutableLiveData(application);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"请求失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                        if("201".equals(ex.getCode())){
                        }
                    }
                });
    }

    public void requestPersonInfo(ApiService apiService,Context context,Long userId){
        apiService.getUser(userId).compose(ResponseResultTransformer.<User>obtain())
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        Log.d(TAG,"请求成功");
                        setUserMutableLiveData(user);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"请求失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                        if("201".equals(ex.getCode())){
                        }
                    }
                });
    }

    public void saveNavToSettlement(){
        MutableLiveData<Navigation> settleNav = LiveDataManager
                .getInstance().with("settleNav", Navigation.class);
        settleNav.setValue(navigationMutableLiveData.getValue());

    }


    public void requestDriverLocation(ApiService apiService, Context context) {
        apiService.getDriverLocation(orderMutableLiveData.getValue().getOrderId())
                .compose(ResponseResultTransformer.<Guide>obtain())
                .subscribe(new Consumer<Guide>() {
                    @Override
                    public void accept(Guide guide) throws Exception {
                        Log.d(TAG,"得到司机位置信息");
                        setGuideMutableLiveData(guide);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"获取司机位置信息失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    public void requestNavigation(ApiService apiService, Context context) {
        apiService.getWaitingNavigation()
                .compose(ResponseResultTransformer.<Navigation>obtain())
                .subscribe(new Consumer<Navigation>() {
                    @Override
                    public void accept(Navigation navigation) throws Exception {
                        Log.d(TAG,"得到导航信息");
                        setNavigationMutableLiveData(navigation);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"获取导航信息失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    public void requestOrder(final ApiService apiService, final Context context) {
        apiService.getSendingOrderStatus()
                .compose(ResponseResultTransformer.<Order>obtain())
                .subscribe(new Consumer<Order>() {
                    @Override
                    public void accept(Order order) throws Exception {
                        Log.d(TAG,"得到订单信息");
                        setOrderMutableLiveData(order);
                        //加载到订单信息后执行
                        requestDriverLocation(apiService,context);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"获取订单信息失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    public void requestGetOff(ApiService apiService,Context context){
        apiService.orderWithGetOff(orderMutableLiveData.getValue().getOrderId())
                .compose(ResponseResultTransformer.<Long>obtain())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Log.d(TAG,"修改订单状态成功");
                        setIsGetOff(true);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"修改订单状态失败！");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }
}
